/*
 * PopupThreadStartWidget.cpp
 *
 *  Created on: Jul 11, 2012
 *      Author: guillou
 */

#include <PopupThreadStartWidget.hpp>
#include <PopupUsersWidget.hpp>
#include <PopupUtilities.hpp>

using namespace std;
using namespace utilities;
using namespace Popup;

PopupThreadStartWidget::PopupThreadStartWidget(QWidget *p_parent)
: QWidget(p_parent), m_usersWidget(new PopupUsersWidget(p_parent))
{
  setupUi(this);
  m_clientsLayout->addWidget(m_usersWidget);
  slot_setupWidget(MODE_DEFAULT);

  connect(m_widgetModeCombo, SIGNAL(currentIndexChanged(int)),
          this, SLOT(slot_setupWidget(int)));
  connect(m_canvassChoiceEdit, SIGNAL(returnPressed()),
          this, SLOT(slot_canvassAddChoice()));
  connect(m_canvassDelChoiceBtn, SIGNAL(clicked()),
          this, SLOT(slot_canvassDeleteChoice()));
}

void PopupThreadStartWidget::getNewThreadInfo(QString & p_threadTitle,
                                              UserList & p_threadAttendants)
{
  m_usersWidget->getSelectedUsers(p_threadAttendants);
  p_threadTitle = m_titleEdit->text();
}

void PopupThreadStartWidget::slot_setupWidget(int p_mode)
{
  bool _showCanvasControls = (p_mode == MODE_CANVASS);
  m_canvassPropertiesBox->setVisible(_showCanvasControls);
  m_canvassChoicesBox->setVisible(_showCanvasControls);
}

PopupThreadStartWidget::Mode PopupThreadStartWidget::getMode() const
{
  return (Mode) m_widgetModeCombo->currentIndex();
}

void PopupThreadStartWidget::getNewCanvassInfo(Canvass & p_canvass)
{
  p_canvass.question = m_titleEdit->text().toUtf8().data();
  p_canvass.isAnonymous = m_anonymousCanvass->isChecked();
  p_canvass.allowMultiSelection = m_multipleChoiceEnabled->isChecked();
  p_canvass.timeout = m_canvassTimeout->value();
  for (int _i = 0; _i < m_canvassChoicesList->count(); _i++) {
    p_canvass.choices.push_back(m_canvassChoicesList->item(_i)->text().toUtf8().data());
  }
  m_usersWidget->getSelectedUsers(p_canvass.targets);
  if (m_isSubmitterAVoter->isChecked()) {
    p_canvass.targets.insert(getMyID());
  }
}

bool PopupThreadStartWidget::validate()
{
  bool _validates = true;

  if (!m_usersWidget->hasSelection()) {
    showError(tr("You must select at least one target!"), this);
    _validates = false;
  }

  if (_validates)
  {
    if (getMode() == MODE_DEFAULT) {
      // No other check!
    }
    else if (getMode() == MODE_CANVASS)
    {
      // Check that question is not empty
      if (m_titleEdit->text().length() == 0) {
        showError(tr("No question, No opinion request!"), this);
        _validates = false;
      }
      // Check that at least 2 choices are available
      else if (m_canvassChoicesList->count() < 2) {
        showError(tr("Needs at least 2 choices!!"), this);
        _validates = false;
      }
    }
  }

  return _validates;
}

void PopupThreadStartWidget::slot_canvassDeleteChoice()
{
  if (m_canvassChoicesList->currentItem() == 0) {
    showError("Please select the element you want to delete, "
              "and then click the delete button.", this);
  } else {
    m_canvassChoicesList->takeItem(m_canvassChoicesList->currentRow());
  }
}

void PopupThreadStartWidget::slot_canvassAddChoice()
{
  QString _newitem = m_canvassChoiceEdit->text();
  if (_newitem.length() == 0) {
    showError("Empty! Empty! Empty!", this);
  } else {
    bool _ok = true;
    for (int _i = 0; _i < m_canvassChoicesList->count(); _i++) {
      if (_newitem.compare(m_canvassChoicesList->item(_i)->text()) == 0) {
        showError("Choice \"" + _newitem + "\" is already in the list!", this);
        _ok = false;
        break;
      }
    }
    if (_ok) {
      m_canvassChoicesList->addItem(_newitem);
      m_canvassChoiceEdit->clear();
    }
  }
}
